package com.bite.system.Service.ServiceImpl;

import Constants.RedisConstant;
import cn.hutool.core.bean.BeanUtil;
import com.bite.system.DTO.QuestionAddDTO;
import com.bite.system.DTO.QuestionDTO;
import com.bite.system.DTO.QuestionEditDTO;
import com.bite.system.ES.QuestionES;
import com.bite.system.ElasticSearch.QuestionRepository;
import com.bite.system.Mapper.QuestionMapper;
import com.bite.system.Service.QuestionService;
import com.bite.system.VO.QuestionAddVO;
import com.bite.system.VO.QuestionVO;
import com.bite.system.domain.Question;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import domain.MyThreadLocal;
import domain.R;
import domain.TableDataInfo;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private StringRedisTemplate redis;

    @Override
    public TableDataInfo list(QuestionDTO questionDTO) {
        String removalQuestionIds = questionDTO.getRemovalQuestionIds();
        if ( removalQuestionIds != null && !removalQuestionIds.equals("")){
            String[] ids = removalQuestionIds.split(";");
            Set<Long> collect = Arrays.stream(ids).map(Long::valueOf).collect(Collectors.toSet());
            questionDTO.setRemoveQuestionIdSet(collect);
        }
        PageHelper.startPage(questionDTO.getPage(), questionDTO.getPageSize());
        List<QuestionVO> list =questionMapper.list(questionDTO);
        if (list != null) {
            
            return TableDataInfo.success(list, ((Page) list).getTotal());
        }
        return TableDataInfo.empty();
    }

    /**
     * 添加题目
     * @param questionAddDTO
     * @return
     */
    @Override
    public R<Void> add(QuestionAddDTO questionAddDTO) {
        Question question = BeanUtil.copyProperties(questionAddDTO, Question.class);
        question.setCreateBy(Long.valueOf(MyThreadLocal.get()));
        question.setUpdateBy(Long.valueOf(MyThreadLocal.get()));
        question.setCreateTime(LocalDateTime.now());
        question.setUpdateTime(LocalDateTime.now());

        int insert = questionMapper.insert(question);
        if (insert > 0){
            //更新es
            questionRepository.save(BeanUtil.copyProperties(question, QuestionES.class));
            //更新redis
            redis.opsForList().leftPush(RedisConstant.QUESTION_LIST_ID,question.getQuestionId().toString());
            return R.success();
        }
        return R.failed();
    }

    @Override
    public R<QuestionAddVO> detail(Long questionId) {
        //先根据ID查询题目信息
        Question question = questionMapper.selectById(questionId);
        if (question == null){
            throw new RuntimeException("题目不存在");
        }
        QuestionAddVO questionAddVO = BeanUtil.copyProperties(question, QuestionAddVO.class);
        return R.success(questionAddVO);
    }

    @Override
    public R<Void> edit(QuestionEditDTO questionEditDTO) {
        Question question = BeanUtil.copyProperties(questionEditDTO, Question.class);
        int i = questionMapper.updateById(question);
        if (i > 0){
            questionRepository.save(BeanUtil.copyProperties(question, QuestionES.class));
            return R.success();
        }

        return R.failed();
    }

    @Override
    public R<Void> delete(Long questionId) {
        int i = questionMapper.deleteById(questionId);
        if (i > 0){
            //更新es
            questionRepository.deleteById(questionId);
            //更新redis
            redis.opsForList().remove(RedisConstant.QUESTION_LIST_ID, 1L, questionId.toString());
            return R.success();
        }
        return R.failed();
    }
}
